#! /usr/bin/python
# -*- coding: cp1250 -*-
#import sys; sys.path.append(r'C:/xxx/yyy')

import sys
import urllib2
import htmllib
import formatter
import string
import urlparse

def isOnline(url):
    ''' Check network resource availability.
    '''
    try:
        response = urllib2.urlopen(url)
    except:
        return False
    else:
        return True

class Url:
    ''' Download, save and process html (URL) files.
    '''
    def __init__(self):
        ''''''
        self.get_htmldata = ''
        self.get_err = ''

    def get(self, url):
        ''' Download html file
        '''
        res = {}
        # could use isOnline() but ...
        try:
            response = urllib2.urlopen(url)
        except:
            err =  'get(\'%s\')'%(url,) + \
                   "\nUnexpected error:" + \
                   str(sys.exc_info()[0])
            #for debuging uncomment
            #raise
        else:
            htmldata = response.read()

        if locals().has_key('htmldata'):
            self.get_htmldata = htmldata
            self.get_err = ''
            return 1
        else:
            self.get_err = err
            return 0

    def saveAsFile(self, outputFile):
        ''''''
        ofile = open(outputFile, 'wb')
        ofile.write(self.get_htmldata)
        ofile.close()

    def getData(self):
        ''''''
        return self.get_htmldata

class HtmlLinks(htmllib.HTMLParser):
    ''' Extract links from html-page
        http://mail.python.org/pipermail/python-list/2000-May/034400.html
    '''

    def __init__(self, base):
        htmllib.HTMLParser.__init__(self, formatter.NullFormatter())

        self.anchors = []
        self.base = base

    def fill(self):
        f = Url()
        f.get(self.base)
        self.feed(f.get_htmldata)

    def anchor_bgn(self, href, name, type):
        self.save_bgn()
        if self.base:
            self.anchor = urlparse.urljoin(self.base, href)
        else:
            self.anchor = href

    def anchor_end(self):
        text = string.strip(self.save_end())
        if self.anchor and text:
            self.anchors.append((self.anchor, text))


if __name__ == '__main__':

    # isOnline() examples
    print isOnline(url='http://www.google.com')
    #print isOnline(r'file:///G:/AFT/aft.html')

    # Url() class example
    #import pepe.p_network.url as url
    u = Url()
    u.get('http://www.python.org')
    u.saveAsFile('xxx.html')

    # HtmlLinks() class example
    p = HtmlLinks("http://www.pythonware.com")
    p.fill()
    print "anchors =", p.anchors
    print "title =", p.title
    print p.title
    
    for a in p.anchors:
        print a[0],' -- '*3 ,a[1]
